package tcln.gxtx.client.widget.grid.menu;

import java.util.Map;

import com.extjs.gxt.ui.client.core.FastMap;
import com.extjs.gxt.ui.client.widget.menu.MenuBar;

/**
 * Extension to {@link MenuBar} to provide more methods:
 * <ul>
 * <li>Get correct menu items in this bar DIRECTLY.
 * <li>Register a menu item for <i>direct-lookup</i>.
 * </ul>
 * 
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class ExtendedMenuBar extends MenuBar {

    /**
     * All menu items in this menu bar. Notice that only items whose class is (or a subclass of)
     * {@link ExtendedMenuItem} can be in this collection.
     */
    private Map<String, ExtendedMenuItem<?>> registeredMenuItemMap = new FastMap<ExtendedMenuItem<?>>();

    /**
     * Registers a menu item into this bar. This helps other screens can lookup for a correct menu item directly via a
     * single menu ID instead of searching through this menubar for just a single one.
     * 
     * @param item
     *            Menu item with a unique ID.
     */
    public void registerMenuItem(ExtendedMenuItem<?> item) {
        if (item != null && item.getMenuId() != null) {
            registeredMenuItemMap.put(item.getMenuId().toString(), item);
        }
    }

    /**
     * Finds out a <b>registered</b> menu item from current bar by menu unique ID.
     * 
     * @param menuId
     * @return
     */
    public ExtendedMenuItem<?> findRegisteredMenuItem(String menuId) {
        return registeredMenuItemMap.get(menuId);
    }
}
